<template>
	<view>
		<!-- 导航 -->
		<view class="nav">
			<view class="nav-left">
				<view class="this-time">上午好</view>
				<view class="this-user">{{userName}}</view>
			</view>
			<view class="nav-right">
				<icon class="nav-search" type="search" size="26" />
				<input type="text" confirm-type="search" v-model="searchText" @confirm="useSearch" placeholder="请输入关键字">
			</view>
		</view>
		<!-- 轮播 -->
		<view class="uni-padding-wrap">
			<view class="page-section swiper">
				<view class="page-section-spacing">
					<swiper class="swiper" indicator-active-color="#0c34ba" indicator-color="#fff" indicator-dots
						autoplay :interval="3000" :duration="1000">
						<swiper-item v-for="(item,index) in bannerList" :key="index">
							<image class="swiper-item-img" :src="item.bannerImg" mode=""></image>
						</swiper-item>
					</swiper>
				</view>
			</view>
		</view>
		<!-- 热卖推荐 -->
		<view class="hot_wrap">
			<view class="hot_box">热卖推荐</view>
		</view>
		<view class="good_wrap">
			<view class="good_box" v-for="(item,index) in goodList" @click="toDo(item)" :key="index">
				<view class="good_img_box">
					<view class="goot_hot">hot</view>
					<image class="good_img" :src="item.smallImg" mode=""></image>
				</view>
				<view class="good_name">
					<text>{{item.name}}</text>
				</view>
				<view class="good_title">
					{{item.enname}}
				</view>
				<view class="good_price">
					<text>￥ {{item.price}}</text>
				</view>
			</view>
		</view>
		
		<view class="u-text-center u-tips-color">
			<text class="">没有更多了..</text>
		</view>

		<!-- 回到顶部 -->
		<view class="back_top" v-show="isShowTop">
			<image @click="useToTop" src="@/static/images/top.png" mode=""></image>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bannerList: '',
				time: '',
				goodList: '',
				isShowTop: false,
				searchText:'',//搜索关键字
				userName:''
			}
		},
		onLoad() {
			this.useGetBanner()
			this.useGetGood()
			this.useGetUserInfo()
		},
		// 页面滚动时触发
		onPageScroll(e) {
			// 隐藏显示回到顶部
			if (e.scrollTop > 100) {
				this.isShowTop = true;
			} else {
				this.isShowTop = false;
			}
		},
		methods: {
			// 获取用户名
			useGetUserInfo(){
				this.$request({
					url: "/findMy",
					success: (res) => {
						// console.log(res)
						if (res.statusCode === 200) {
							uni.hideLoading()
							if (res.data.code === "A001") {
								this.userName = res.data.result[0].nickName || '小瑞幸'
							}
						}
					}
				})
			},
			// 搜索
			useSearch(){
				uni.navigateTo({
					url:`../search/search?text=${this.searchText}`
				})
			},
			// 回到顶部
			useToTop() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 100
				})
			},
			// 获取商品
			useGetGood() {
				uni.request({
					url: "http://www.kangliuyong.com:10002/typeProducts",

					data: {
						appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
						key: 'isHot',
						value: 1

					},
					success: (res) => {
						if (res.statusCode === 200) {
							this.goodList = res.data.result
						}

					}
				})
			},

			// 获取首页轮播
			useGetBanner() {
				uni.request({
					url: "http://www.kangliuyong.com:10002/banner",

					data: {
						appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA="
					},
					success: (res) => {
						if (res.statusCode === 200) {
							this.bannerList = res.data.result
						}

					}
				})
			},
			// 判断时段
			useJudgeTime() {
				//判断  时间
				let time = new Date().getHours()
				if (6 < time && time < 12) {
					this.time = "早上好"
				} else if (12 < time && time < 18) {
					this.time = "下午好"
				} else if (18 < time && time < 24) {
					this.time = "晚上好"
				} else if (time == 12) {
					this.time = "中午好"
				} else {
					this.time = "深夜了"
				}
			},
			// 跳转详情页
			toDo(item) {
				uni.navigateTo({
					url: `../detail/detail?pid=${item.pid}`
				})
			}
		},
	}
</script>

<style lang="less" scoped>
	page {
		background-color: #f1f1f1;
	}

	.nav {
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #fff;

		.nav-left {
			flex: 3;
			display: flex;
			justify-content: space-between;

			.this-time {
				font-weight: bold;
			}

			.this-user {
				color: #00007f;
				font-weight: bold;
			}
		}

		.nav-right {
			flex: 7;
			margin-left: 60rpx;
			display: flex;
			justify-content: space-evenly;
			align-items: center;
			background-color: #f5f5f5;
			border-radius: 10rpx;
			padding: 8rpx;

			::v-deep.uni-icon-search {
				font-size: 32rpx !important;
			}
		}
	}

	.swiper {
		border-radius: 20rpx;
		overflow: hidden;
		height: 460rpx;
		margin: 10rpx;

		.uni-swiper-wrapper {}

		.swiper-item-img {
			width: 100%;
			height: 100%;
		}

	}

	::v-deep .uni-swiper-dots-horizontal {
		position: absolute;
		bottom: 30rpx;
		left: 84%;

		.uni-swiper-dot {
			width: 26rpx;
			height: 6rpx;
			border-radius: 0;
		}
	}

	// 热卖推荐
	.hot_wrap {
		margin: 20rpx;
		padding: 12px 0;
		background-color: #fff;

		.hot_box {
			background-color: #103fb9;
			color: #fff;
			padding: 10rpx 28rpx;
			border-radius: 0 40rpx 0 0;
			display: inline-block;
			font-size: 28rpx;
		}
	}

	// 商品
	.good_wrap {
		display: flex;
		margin: 16rpx;
		flex-flow: row wrap;

		.good_box {
			border-radius: 14rpx;
			margin: 1%;
			background-color: #fff;
			flex: 48%;
			padding: 16rpx;
			box-sizing: border-box;
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.good_img_box {
				position: relative;
				border-radius: 14rpx;
				overflow: hidden;

				&>image {
					width: 100%;
					height: 240rpx;
				}

				.goot_hot {
					background-color: #103fb9;
					color: #fff;
					display: inline-block;
					position: absolute;
					top: 0;
					left: 20rpx;
					z-index: 99;
					border-radius: 0 0 10rpx 10rpx;
					padding: 10rpx 4rpx;
				}

			}

			.good_name {
				font-size: 36rpx;
				color: gray;
				margin: 10rpx 0;
			}

			.good_title {
				font-size: 24rpx;
				color: gray;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
				width: 280rpx;
			}

			.good_price {
				font-size: 34rpx;
				color: #103fb9;
				margin: 10rpx 0;
				font-weight: bold;
			}
		}
	}

	// 回到顶部
	.back_top {
		position: fixed;
		right: 40rpx;
		bottom: 140rpx;
		z-index: 999;

		image {
			width: 100rpx;
			height: 100rpx;
		}
	}
</style>
